package csl.mq.rabbitmq.old.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;

/**
 * @Description: TODO
 * @author: 隔山海
 * @date: 2021年08月29日 19:22
 */
//@Configuration
@Slf4j
public class rabbitMQConfig {

    @Value("${mq.host}")
    private String address;

    @Value("${mq.username}")
    private String username;

    @Value("${mq.password}")
    private String password;

    @Value("${mq.virtual.host}")
    private String virtualHost;

//    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setAddresses(address);
        cachingConnectionFactory.setUsername(username);
        cachingConnectionFactory.setPassword(password);
        cachingConnectionFactory.setVirtualHost(virtualHost);
        //发送确认
//        cachingConnectionFactory.setPublisherConfirms(true);
        //路由失败回调
//        cachingConnectionFactory.setPublisherReturns(true);
        log.info("Init RabbitMQ ConnectionFactory successful!");
        return cachingConnectionFactory;
    }

    /**
     * 异步任务Sender
     *
     * @param <T>
     * @return
     * @throws IOException
     */
//    @Bean
//    public <T extends BaseMessage> MessageSender<T> eventSender() throws IOException {
//        AsyncEventAccessBuilder<T> asyncEventAccessBuilder = new AsyncEventAccessBuilder<>();
//        return asyncEventAccessBuilder.buildMessageSender(
//                eventExchange,
//                eventDeadExchange,
//                routingKey,
//                eventQueue,
//                type);
//    }

}
